$('#go-reg').on('click', function () {
    $('.reg-content').show()
    $('.login-content').hide()
})


$('#go-login').on('click', function () {
    $('.reg-content').hide()
    $('.login-content').show()
})

const form = layui.form
form.verify({
    username: [/^[a-z0-9]{5,10}$/i, '用户名必须为5-10位的数字或字母'],
    password: [/^\S{6,12}$/, '密码必须为6-12, 并且不能为空格!'],
    rpPassword: function (value) {
        const pwd = $('.reg-content [name=password]').val()
        if (value !== pwd) {
            return '两次密码不相同'
        }
    }
})

const layer = layui.layer
$('#reg-form').on('submit', function() {
    const data = form.val('reg-form')
    $.post('/api/reguser', data)
        .then(res => {
            if (res.status !== 0) {
                return layer.msg(res.message || '注册失败')
            }
            layer.msg('注册成功')
            this.reset()
            $('#go-login').click()
        })
    return false
})

$('#login-form').on('submit', function(){
    const data = form.val('login-form')
    $.post('/api/login',data)
    .then(res => {
        if(res.status !== 0){
            return layer.msg(res.message || '登入失败')
        }
        layer.msg('登入成功')
        localStorage.setItem('BIG_EVENT_TOKEN', res.token)
        location.href = '/index.html'
    })
    return false
})